package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @autahor: csl
 * @date: 2019/7/18
 * @description:
 * @version:1.0
 */
@RestController
@Slf4j
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService service;
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] ids){
         try {
                     service.add(checkGroup,ids);
                     return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
                 }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.ADD_CHECKGROUP_FAIL);
                 }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
         try {
             return service.findPage(queryPageBean);

         }catch (Exception e){
                     e.printStackTrace();
                     return new PageResult(0L,new ArrayList());
                 }
    }
    @RequestMapping("/findCheckItemByCheckGroupId")
    public Result findCheckItemByCheckGroupId(Integer id){
         try {
             Integer[] ids = service.findCheckItemByCheckGroupId(id);
             return new Result(true,MessageConst.ACTION_SUCCESS,ids);
         }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.ACTION_FAIL);
                 }
    }
    @RequestMapping("/findCheckGroupByCheckGroupId")
    public Result findCheckGroupByCheckGroupId(Integer id){
         try {
             CheckGroup checkGroup = service.findCheckGroupByCheckGroupId(id);
             return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
         }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
                 }
    }
    @RequestMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] ids){
         try {
                     service.update(checkGroup,ids);
                     return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
                 }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
                 }
    }
    @RequestMapping("/findAll")
    public Result findAll(){
         try {
                 List<CheckGroup> list =service.findAll();
                 if (list!=null){
                     return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,list);
                 }else {
                     return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
                 }
                 }catch (Exception e){
                     e.printStackTrace();
                     return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
                 }
    }
}

